<?php

namespace App\Http\Controllers\CmsApi;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Controllers\BaseController;
use App\Http\Services\UserVerifyServices;
use App\Http\Services\BankBlsServices;
use App\Http\Services\FinanceServices;
use App\Http\Services\ApiServices;
use App\Http\Services\FinanceWithdrawServices;
use App\Http\Services\FinanceWithdrawNewServices;
use App\Http\Services\UserServices;

class InnerFinanceController extends BaseController
{
    protected $banBls_service;
    protected $finance_service;
    protected $bankBls_service;
    protected $service_withdraw;
    protected $withdrawNewService;
    protected $userService;

    protected $apiservie;//基础服务

    public function __construct(FinanceWithdrawServices $financeWithdrawServices)
    {
        $this->banBls_service = new BankBlsServices();
        $this->finance_service = new FinanceServices();
        $this->bankBls_service = new BankBlsServices();
        $this->service_withdraw = $financeWithdrawServices;
        $this->withdrawNewService = new FinanceWithdrawNewServices();
        $this->userService = new UserServices();

        $this->apiservie = new ApiServices();//api基础服务
    }

    /**
     * 后台审核同意慧用工提现操作
     * */
    public function withdrawPayYuqueOrder(Request $request){
        $orderSn = (string) $request->get('order_sn');
        if(empty($orderSn)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $rest = $this->withdrawNewService->withdrawPayYuqueOrder($orderSn);
        Response::returnResult($rest['code'], $rest['data'], $rest['msg']);
    }

    /**
     * 后台审核同意支付宝提现操作
     * */
    public function withdrawPayAlipayOrder(Request $request){
        $orderSn = (string) $request->input('order_sn');
        if(empty($orderSn)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $rest = $this->withdrawNewService->withdrawPayAlipayOrder($orderSn);
        Response::returnResult($rest['code'], [], $rest['msg']);
    }

    /**
     * 清理用户银行卡绑定
     */
    public function clearUserbankAll(Request $request)
    {
        $rules = [
            'user_id' => 'required',
        ];
        $messages = [
            'user_id' => '请输入用户ID',
        ];
        $params = parent::validateParams($request->all(), $rules, $messages);
        $this->banBls_service->clearUserbankAll($params['user_id']);
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 全家总动员用户押注扣除金豆
     */
    public function fcJdDecr(Request $request)
    {
        $user_id = (int) $request->post('user_id');
        $change_value = (int) $request->post('change_value');
        $data_id = (int) $request->post('data_id');
        if(empty($user_id) || empty($change_value) || empty($data_id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->fcJdDecr($user_id, $change_value, $data_id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 全家总动员批量奖励用户金豆
     */
    public function fcJdIncreaseBatch(Request $request)
    {
        $data = $this->finance_service->fcJdIncreaseBatch();
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 全家总动员批量发飘萍
     */
    public function fcFullScreenBatch(Request $request)
    {
        $data = $this->finance_service->fcFullScreenBatch();
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 西天取经发飘萍
     */
    public function xtqjFullScreen(Request $request)
    {
        $type = $request->post('type');//飘萍ID
        $param = $request->all();
        $data = $this->finance_service->xtqjFullScreen($type, $param);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 西天取经发礼物及金豆
     */
    public function sendGiftJd(Request $request)
    {
        $param = $request->all();
        $data = $this->finance_service->sendGiftJd($param);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 账户减去钻石操作
     *
     * @param Request $request
     * @return void
     */
    public function diamonDecrease(Request $request)
    {
        $userId = $request->input('user_id');
        $type = $request->input('type');
        $amount = $request->input('amount');
        $description = $request->input('description', '');
    
        $res = $this->apiservie->decrease($userId, $type, $amount, $description);
        if(!$res) {
            Response::returnResult(Hint::ERROR);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 荒漠捕牛变更幸运券
     */
    public function lcGiftNumChange(Request $request)
    {
        $userId = (int) $request->post('userId');
        $giftId = (int) $request->post('giftId');
        $changeValue = (int) $request->post('changeValue');
        if(empty($userId) || empty($giftId) || empty($changeValue)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->lcGiftNumChange($userId, $giftId, $changeValue);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 荒漠捕牛批量发飘萍
     */
    public function lcFullScreenBatch(Request $request)
    {
        $param = $request->post('param');
        if(empty($param)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $param = json_decode($param, true);

        $data = $this->finance_service->lcFullScreenBatch($param);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 开运装备星发飘萍
     */
    public function equipFullScreen(Request $request)
    {
        $type = $request->post('type');//飘萍ID
        $param = $request->all();
        $data = $this->finance_service->equipFullScreen($type, $param);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 开运装备星抽奖扣除金豆
     */
    public function equipJdDecr(Request $request)
    {
        $user_id = (int) $request->post('user_id');
        $change_value = (int) $request->post('change_value');
        $data_id = (int) $request->post('data_id');
        if(empty($user_id) || empty($change_value) || empty($data_id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->equipJdDecr($user_id, $change_value, $data_id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }






    /**
     * @desc 查询用户提现基础信息
     * */
    public function getBankUserBase(Request $request)
    {
        $userId = $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $rest = $this->bankBls_service->getBankUserBase($userId);
        if($rest['code'] != Hint::SUCCESS) {
            Response::returnResult($rest['code'], [], $rest['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $rest['data']);
    }

    /**
     * @desc 绑定银行卡
     * */
    public function bindBank(Request $request)
    {
        $province = $request->get('province');
        $openBank = $request->get('openBank');
        $bankNo = $request->get('bankNo');
        $phone = $request->get('phone');
        $bankNo = str_replace(" ", "", $bankNo);
        if(empty($province) || empty($openBank) || empty($bankNo) || empty($phone)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $userId = $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $rest = $this->bankBls_service->bindBank($userId, $province, $openBank, $bankNo, $phone);
        if($rest['code'] != Hint::SUCCESS) {
            Response::returnResult($rest['code'], [], $rest['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * @desc 银行卡列表
     * */
    public function bankList(Request $request)
    {
        $userId = $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $rest = $this->bankBls_service->bankList($userId);
        if($rest['code'] != Hint::SUCCESS) {
            Response::returnResult($rest['code'], [], $rest['msg']);
        }

        Response::returnResult(Hint::SUCCESS, $rest['data']);
    }

    /**
     * @desc 银行卡删除
     * */
    public function bankDelete(Request $request)
    {
        $userId = $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $bankId = $request->get('bankId');
        if(empty($bankId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $rest = $this->bankBls_service->bankDelete($userId, $bankId);
        if($rest['code'] != Hint::SUCCESS) {
            Response::returnResult($rest['code'], [], $rest['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 是否可以提现
     * */
    public function checkWithdraw(Request $request){
        $userId = $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $rest = $this->service_withdraw->checkWithdraw($userId);
        if($rest['code'] != Hint::SUCCESS) {
            Response::returnResult($rest['code'], [], $rest['msg']);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 查看提现记录
     */
    public function getWithdrawLogV2(Request $request){
        $userId = $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $page = (int) $request->get('page', 0);
        $pageSize = (int) $request->get('pageSize', 20);

        $rest = $this->service_withdraw->getWithdrawLogV2($userId, $page, $pageSize);
        Response::returnResult($rest['code'], $rest['data']);
    }

    /**
     * h5 申请提现
     * */
    public function applyWithdrawV2(Request $request){
        $userId = $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $bankId = (int) $request->input('bankId');
        $price = (int) $request->input('price');
        $payType = (int) $request->input('pay_type', 7);
        if(empty($bankId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if($price < 100) {
            Response::returnResult(Hint::PARAM_ERROR, [], '提现金额需满100');
        }
        if(!($price%10==0)) {
            Response::returnResult(Hint::PARAM_ERROR, [], '每次提现须为10的倍数');
        }
        //账号类型 7=银行卡-贝灵斯 8=支付宝-慧用工 9=支付宝-提现
        if($payType < 7) $payType = 7;//兼容旧代码
        if(!in_array($payType, [7,8])) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        if($payType == 8) $payType = 9;//慧用工变成支付宝

        $rest = $this->service_withdraw->applyWithdrawV2($userId, $bankId, $price, $payType);
        if($rest['code'] != Hint::SUCCESS) {
            $msg = empty($rest['data']['error_msg']) ? "" : $rest['data']['error_msg'];
            Response::returnResult($rest['code'], [], $msg);
        }
        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 用户提现支付宝保存
     */
    public function saveUserAlipay(Request $request)
    {
        $userId = $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $userInfo = $this->userService->getUserData($userId, true);
        if(empty($userInfo)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $workerAccount = trim($request->input('worker_account'));
        if(empty($workerAccount)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $res = $this->withdrawNewService->saveUserAlipay($userId, $userInfo['phone'], $workerAccount);

        Response::returnResult($res['code'], $res['data'], $res['msg']);
    }

    /**
     * 用户提现支付宝删除
     */
    public function delUserAlipay(Request $request)
    {
        $userId = $request->input('user_id');
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $res = $this->withdrawNewService->delUserAlipay($userId);

        Response::returnResult($res['code'], $res['data'], $res['msg']);
    }

    /**
     * 充值黑名单新增
     */
    public function rechargeBlackAdd(Request $request)
    {
        $userId = (int) $request->input('user_id');
        $content = (string) $request->input('content');
        $payType = (int) $request->input('pay_type');
        if(empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->rechargeBlackAdd($userId, $content, $payType);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 充值黑名单删除
     */
    public function rechargeBlackDel(Request $request)
    {
        $id = (int) $request->input('id');
        if(empty($id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->rechargeBlackDel($id);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 临时充值黑名单删除
     */
    public function shortRechargeBlackDel(Request $request){
        $id = (int) $request->input('id');
        if(empty($id)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->shortRechargeBlackDel($id);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 临时充值黑名单新增
     *
     */
    public function shortRechargeBlackAdd(Request $request){
        $userId = (int) $request->input('user_id');
        $content = (string) $request->input('content');
        $payType = (int) $request->input('pay_type');
        if(empty($userId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->shortRechargeBlackAdd($userId, $content, $payType);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 留言板消耗钻石发留言
     */
    public function lybDiamandDecr(Request $request)
    {
        $userId = (int) $request->input('user_id');//用户ID
        $toUserId = (int) $request->input('to_user_id');//收用户id
        $num = (int) $request->input('num');//消耗钻石数量
        $content = (string) $request->input('content');//留言内容
        $dataType = (int) $request->input('data_type');//类型
        $dataId = (int) $request->input('data_id');//数据ID
        if(empty($userId) || empty($toUserId) || empty($content)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->lybDiamandDecr($userId, $toUserId, $num, $content, $dataType, $dataId);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 新年活动赠送金豆
     */
    public function newYearJdIncreaseBatch(Request $request){
        $user_id = (int) $request->post('user_id');
        $change_value = (int) $request->post('jd_value');
        $data_id = (int) $request->post('data_id');
        if(empty($user_id) || empty($change_value)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->finance_service->newYear2024JdIncr($user_id, $change_value, $data_id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 新年活动赠送/扣减人气票
     */
    public function newYearGiftNumChange(Request $request)
    {
        $userId = (int) $request->post('userId');
        $giftId = (int) $request->post('giftId');
        $changeValue = (int) $request->post('changeValue');
        $type = (int) $request->post('type',2);
        if(empty($userId) || empty($giftId) || empty($changeValue)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->newYearGiftNumChange($userId, $giftId, $changeValue, $type);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 送彩蛋礼物奖励碎片
     */
    public function colorGiftDebrisIncr(Request $request)
    {
        $userId = (int) $request->post('userId');
        $giftId = (int) $request->post('giftId');
        $changeValue = (int) $request->post('changeValue');
        $giftNum = (int) $request->post('giftNum');
        if(empty($userId) || empty($giftId) || empty($changeValue) || empty($giftNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->colorGiftDebrisIncr($userId, $giftId, $changeValue, $giftNum);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 购买商品扣除碎片
     */
    public function buyGoodDebrisDecr(Request $request)
    {
        $userId = (int) $request->post('userId');
        $goodId = (int) $request->post('goodId');
        $changeValue = (int) $request->post('changeValue');
        $dayNum = (int) $request->post('dayNum');
        if(empty($userId) || empty($goodId) || empty($changeValue) || empty($dayNum)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->buyGoodDebrisDecr($userId, $goodId, $changeValue, $dayNum);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 三国群英扣除钻石
     */
    public function lcDiamondDecr(Request $request)
    {
        $user_id = (int) $request->input('user_id');
        $change_value = (int) $request->input('change_value');
        if(empty($user_id) || empty($change_value)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->lcDiamondDecr($user_id, $change_value);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 三国群英撤回增加钻石
     */
    public function lcBackDiamondIncr(Request $request)
    {
        $user_id = (int) $request->input('user_id');
        $change_value = (int) $request->input('change_value');
        if(empty($user_id) || empty($change_value)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->lcBackDiamondIncr($user_id, $change_value);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 三国群英押注扣除礼物或钻石
     */
    public function lcDiamondGiftDecr(Request $request)
    {
        $user_id = (int) $request->input('user_id');
        $gift_id = (int) $request->input('gift_id');
        $gift_num = (int) $request->input('gift_num');
        $gift_price = (int) $request->input('gift_price');
        if(empty($user_id) || empty($gift_id) || empty($gift_num) || empty($gift_price)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->lcDiamondGiftDecr($user_id, $gift_id, $gift_num, $gift_price);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 三国群英撤回增加礼物
     */
    public function lcBackGiftIncr(Request $request)
    {
        $user_id = (int) $request->input('user_id');
        $gift_id = (int) $request->input('gift_id');
        $gift_num = (int) $request->input('gift_num');
        if(empty($user_id) || empty($gift_id) || empty($gift_num)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->lcBackGiftIncr($user_id, $gift_id, $gift_num);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 三国群英批量奖励用户指定礼物
     */
    public function lcGiftNumChangeBatch(Request $request)
    {
        $param = $request->input('param');
        $gameNum = (int) $request->input('gameNum');
        if(empty($param)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $param = json_decode($param, true);

        $data = $this->finance_service->lcGiftNumChangeBatch($param, $gameNum);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 三国群英批量发飘萍
     */
    public function lcFullScreenBatchV2(Request $request)
    {
        $param = $request->post('param');
        if(empty($param)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $param = json_decode($param, true);

        $data = $this->finance_service->lcFullScreenBatchV2($param);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 自定义活动榜单奖励金豆
     */
    public function customRankJdIncr(Request $request)
    {
        $user_id = (int) $request->post('user_id');
        $change_value = (int) $request->post('change_value');
        $data_id = (int) $request->post('data_id');
        if(empty($user_id) || empty($change_value)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->customRankJdIncr($user_id, $change_value, $data_id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 自定义活动-累充发放金豆
     */
    public function customRechargeJdIncr(Request $request){
        $user_id = (int) $request->post('user_id');
        $change_value = (int) $request->post('jd_value');
        $data_id = (int) $request->post('data_id');
        if(empty($user_id) || empty($change_value)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->customRechargeJdIncr($user_id, $change_value, $data_id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }

    /**
     * 自定义累充活动领取钻石
     */
    public function rechargeDiamondIncr(Request $request){
        $user_id = (int) $request->post('user_id');
        $change_value = (int) $request->post('jd_value');
        $data_id = (int) $request->post('data_id');
        if(empty($user_id) || empty($change_value)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->finance_service->rechargeDiamondIncr($user_id, $change_value, $data_id);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }

        Response::returnResult(Hint::SUCCESS);
    }
}